package com.guli.edu.controller.admin;

import com.guli.common.constants.ResultCodeEnum;
import com.guli.common.exception.GuliException;
import com.guli.common.vo.R;
import com.guli.edu.service.SubjectService;
import com.guli.edu.vo.SubjectNestedVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author XiaoHua666
 * @date 2019/4/20
 */
@Api(tags = "课程分类管理")
@RestController
@CrossOrigin
@RequestMapping(value = "/admin/edu/subject")
public class SubjectAdminController {
    @Autowired
    private SubjectService subjectService;

    @ApiOperation(value = "Excel批量导入")

    @PostMapping(value = "import")
    public R batchImport(
            @ApiParam(value = "Excel分类文件", name = "file", required = true)
            @RequestParam("file") MultipartFile file

    ) {
        try {
            List<String> errorMsg = subjectService.bachImport(file);
            if (errorMsg.size() == 0) {
                return R.ok().message("批量导入成功");
            } else {
                return R.error().message("部分导入成功").data("errorMsgList", errorMsg);
            }
        } catch (Exception e) {
            throw new GuliException(ResultCodeEnum.EXCEL_DATA_IMPORT_ERROR);
        }
    }

    @ApiOperation(value = "嵌套数据列表")
    @GetMapping("")
    public R nestedList(){

        List<SubjectNestedVo> subjectNestedVoList = subjectService.nestedList();
        return R.ok().data("items", subjectNestedVoList);
    }

}

